package Limit.BuffDoll;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;

import l1j.server.L1DatabaseFactory;
import l1j.server.server.utils.SQLUtil;

public class BuffDoll_Info_Table {
	
	private static BuffDoll_Info_Table _instance;
	
	private final HashMap<Integer, BuffDoll_Info_Load> _DollInfo = new HashMap<Integer, BuffDoll_Info_Load>();
	
	public static BuffDoll_Info_Table getInstance() {
		if (_instance == null) {
			_instance = new BuffDoll_Info_Table();
		}
		return _instance;
	}
	
	public boolean countBuffDoll(int itemid)
	{
		boolean result = false;
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try 
		{
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con
					.prepareStatement("SELECT * FROM buffdoll_info WHERE Item_ID =?");
			pstm.setInt(1, itemid);
			rs = pstm.executeQuery();
			
			if (rs.next()) 
			{
				result = true;
			}
		} 
		catch (SQLException e) {System.out.print(e.getMessage());} 
		finally {SQLUtil.close(rs);SQLUtil.close(pstm);SQLUtil.close(con);}
		return result;
	}
	
	public void StorageBuffDoll(int itemid)
	{	
		Connection con = null;	
		PreparedStatement pstm = null;
		
		try {
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con
					.prepareStatement("INSERT INTO buffdoll_info SET Item_ID=?");
			pstm.setInt(1, itemid);
			pstm.execute();
		} 
		catch (Exception e) 
		{
			System.out.print(e.getMessage());
		} finally 
		{
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
	}
	
	public void loadBuffDoll_info(int itemid)
	{	
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try 
		{
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("SELECT * FROM buffdoll_info WHERE Item_ID = ?");
			pstm.setInt(1, itemid);
			rs = pstm.executeQuery();
			
			while (rs.next())
			{
				BuffDoll_Info_Load info = new BuffDoll_Info_Load();
				int id = rs.getInt("Item_ID");
				info.setItemID(id);
				info.setLevel(rs.getInt("Level"));
				info.setPercent(rs.getDouble("Percent"));
				info.setPlace1(rs.getInt("Place_1"));
				info.setPlace2(rs.getInt("Place_2"));
				info.setPlace3(rs.getInt("Place_3"));
				info.setPlace4(rs.getInt("Place_4"));
				info.setPlace5(rs.getInt("Place_5"));
				info.setPlace6(rs.getInt("Place_6"));
				info.setDoll1(rs.getInt("Doll_1"));
				info.setDoll2(rs.getInt("Doll_2"));
				info.setPlace1Use(rs.getInt("Place_1_Use") == 1 ? true : false);
				info.setPlace2Use(rs.getInt("Place_2_Use") == 1 ? true : false);
				info.setPlace3Use(rs.getInt("Place_3_Use") == 1 ? true : false);
				info.setPlace4Use(rs.getInt("Place_4_Use") == 1 ? true : false);
				info.setPlace5Use(rs.getInt("Place_5_Use") == 1 ? true : false);
				info.setPlace6Use(rs.getInt("Place_6_Use") == 1 ? true : false);
				info.setPlace5Pay(rs.getInt("Place_5_Pay") == 1 ? true : false);
				info.setPlace6Pay(rs.getInt("Place_6_Pay") == 1 ? true : false);
				info.setDoll2Pay(rs.getInt("Doll_2_Pay") == 1 ? true : false);
				info.setAuto_Res(rs.getInt("Auto_Res") == 1 ? true : false);
				info.setChangePlace(rs.getInt("ChangePlace"));
				info.setChangeDoll(rs.getInt("ChangeDoll"));
				info.setMP(rs.getInt("MP"));

				_DollInfo.put(new Integer(id), info);
			}
		}
		
		catch (SQLException e) 
		{
			System.out.print(e.getMessage());
		}
		finally 
		{
			SQLUtil.close(rs, pstm, con);
		}
	}
	
	public void updateBuffDoll_info(int itemid, BuffDoll_Info_Load load)
	{	
		Connection con = null;
		PreparedStatement pstm = null;
		try 
		{
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("UPDATE buffdoll_info SET Level =?, Percent =?, Place_1 =?, Place_2 =?, Place_3 =?, Place_4 =?, Place_5 =?, Place_6 =?, Doll_1 =?, Doll_2 =?" +
					", Place_1_Use =?, Place_2_Use =?, Place_3_Use =?, Place_4_Use =?, Place_5_Use =?, Place_6_Use =?, Place_5_Pay =?, Place_6_Pay =?, Doll_2_Pay =?, Auto_Res =?, ChangePlace =?, ChangeDoll =?, MP =? WHERE Item_ID=?");
			int i = 0;
			pstm.setInt(++i, load.getLevel());
			pstm.setDouble(++i, load.getPercent());
			pstm.setInt(++i, load.getPlace1());
			pstm.setInt(++i, load.getPlace2());
			pstm.setInt(++i, load.getPlace3());
			pstm.setInt(++i, load.getPlace4());
			pstm.setInt(++i, load.getPlace5());
			pstm.setInt(++i, load.getPlace6());
			pstm.setInt(++i, load.getDoll1());
			pstm.setInt(++i, load.getDoll2());
			pstm.setBoolean(++i, load.getPlace1Use());
			pstm.setBoolean(++i, load.getPlace2Use());
			pstm.setBoolean(++i, load.getPlace3Use());
			pstm.setBoolean(++i, load.getPlace4Use());
			pstm.setBoolean(++i, load.getPlace5Use());
			pstm.setBoolean(++i, load.getPlace6Use());
			pstm.setBoolean(++i, load.getPlace5Pay());
			pstm.setBoolean(++i, load.getPlace6Pay());
			pstm.setBoolean(++i, load.getDoll2Pay());
			pstm.setBoolean(++i, load.getAuto_Res());
			pstm.setInt(++i, load.getChangePlace());
			pstm.setInt(++i, load.getChangeDoll());
			pstm.setInt(++i, load.getMP());
			pstm.setInt(++i, itemid);
			pstm.execute();
		}
		
		catch (SQLException e) 
		{
			System.out.print(e.getMessage());
		}
		finally 
		{
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
	}
	
	public void delTemplate(int itemID) {
		_DollInfo.remove(itemID);
	}
	
	public BuffDoll_Info_Load getTemplate(int itemid) {
		if (_DollInfo.containsKey(itemid)) {
			return _DollInfo.get(itemid);
		}
		return null;
	}
}
